package com.xyzh05.util;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.Assert;

public class HQLHelper implements Serializable {

	/**
	 * 去除HQL的order by 子句，用于pagedQuery.
	 * 
	 * @param hql
	 * @return
	 */
	static String removeOrders(String hql) {
		Assert.hasText(hql);
		Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*",
				Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(hql);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, "");
		}
		m.appendTail(sb);
		return sb.toString();
	}

	/**
	 * 去除HQL的select子句，未考虑union的情况,用于pagedQuery.
	 * 
	 * @param hql
	 * @return
	 */
	static String removeSelect(String hql) {
		return removeSelect(hql, null);
	}

	/**
	 * 去除HQL的select 子句，未考虑union的情况,用于pagedQuery.
	 * 
	 * @param hql
	 * @param i
	 * @return
	 */
	static String removeSelect(String hql, Integer i) {
		Assert.hasText(hql);
		int beginPos = -1;
		if (i == null) {
			beginPos = hql.toLowerCase().lastIndexOf("from");
		} else {
			String[] s = hql.toLowerCase().split("from");
			int number = 0;
			for (int a = 0; a < s.length - i; a++) {
				number = number + s[a].length();
			}
			beginPos = number + 4 * (s.length - i - 1);
		}
		Assert.isTrue(beginPos != -1, " hql : " + hql
				+ " must has a keyword 'from'");
		return hql.substring(beginPos);
	}

	/**
	 * 去除HQL的select 子句并且添加select count(*),
	 * 未考虑union的情况,用于pagedQuery.并且去除HQL的order by 子句，用于pagedQuery.
	 * 
	 * @param hql
	 * @param ii
	 * @return
	 */
	public static String getCountHql(String hql, Integer ii) {
		Assert.hasText(hql);
		int i = hql.indexOf("distinct");
		if (i > -1) {
			return new StringBuffer("select count(distinct c.id) ").append(
					removeSelect(removeOrders(hql), ii)).toString();
		} else {
			return new StringBuffer("select count(*) ").append(
					removeSelect(removeOrders(hql), ii)).toString();
		}
	}

	/**
	 * 去除HQL的select子句并且添加select count(*), 未考虑union的情况,用于pagedQuery.并且去除HQL的order
	 * by 子句，用于pagedQuery.
	 * 
	 * @param hql
	 * @return
	 */
	public static String getCountHql(String hql) {
		return getCountHql(hql, null);
	}

	/**
	 * 去除SQL的select 子句并且添加select count(*) 未考虑union的情况,用于pagedQuery.并且去除SQL的order
	 * by 子句，用于pagedQuery.
	 * 
	 */
	public static String getCountSql(String sql) {
		return getCountSql(sql, null);
	}

	/**
	 * 去除SQL的select 子句并且添加select count(*) 未考虑union的情况,用于pagedQuery.并且去除SQL的order
	 * by 子句,用于pagedQuery.
	 * 
	 */
	public static String getCountSql(String sql, Integer ii) {
		Assert.hasText(sql);
		int i = sql.indexOf("distinct");
		if (i > -1) {
			return new StringBuffer("select count(distinct c.id) ").append(
					removeSelect(removeOrders(sql), ii)).toString();
		} else {
			return new StringBuffer("select count(*) ").append(
					removeSelect(removeOrders(sql), ii)).toString();
		}
	}

	/**
	 * 去除SQL的 select子句，未考虑union的情况,用于pagedQuery. 并且去除SQL的order by
	 * 子句,用于pagedQuery.
	 * 
	 */
	static String removeSelectAndOrder(String hql) {
		return removeSelect(removeOrders(hql));
	}

}
